from ..log import log_gui
from item import BaseFormItem
import gtk
import pango

def EscapeHtml(str):
  return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

class TextFormItem(BaseFormItem):
  WEIGHT_NORMAL = None
  WEIGHT_BOLD = "bold"
  def __init__(self, screen, caption, text_attrs):
    # text_attrs is a dict having these entries (examples given are defaults):
    #   font: Font name, e.g., "Helvetica"
    #   fg_color: Foreground color, e.g., "#000000"
    #   bg_color: Foreground color, e.g., "#FFFFFF"  (None means transparent)
    #   size: Point size, e.g., 12
    #   weight: None (or WEIGHT_BOLD)
    self._layout = TextFormItem.CreateLayout(screen, caption, text_attrs)
    self.caption = caption
    self.text_attrs = dict(text_attrs)
    self.screen = screen
    w, h = self._layout.get_pixel_size()
    BaseFormItem.__init__(self, 0, 0, w, h)
    self.SetInteractive(False)
    self._background_cmap_color = None

  def __str__(self):
    return "<TextFormItem caption=\"%s\">" % self.caption

  def SetCaption(self, caption):
    self.caption = caption
    self._layout = TextFormItem.CreateLayout(
        self.screen, caption, self.text_attrs)
    w, h = self._layout.get_pixel_size()
    self.SetSize(w, h)

  def Draw(self, screen, window, rect, clip_rect):
    self.DrawBackground(screen, window, rect, clip_rect)
    self.DrawForeground(screen, window, rect, clip_rect)

  def DrawBackground(self, screen, window, rect, clip_rect):
    bg_color = self.text_attrs.get('bg_color', '#FFFFFF')
    if bg_color is not None:
      if not self._background_cmap_color:
        cmap = window.get_colormap()
        self._background_cmap_color = cmap.alloc_color(bg_color)
      colorgc = screen.GC(window)
      colorgc.set_foreground(self._background_cmap_color)
      colorgc.set_fill(gtk.gdk.SOLID)

      if clip_rect:
        colorgc.set_clip_rectangle(clip_rect)

      # Draw the background color
      window.draw_rectangle(
          colorgc, True, rect.left, rect.top, self.rect.width,
          self.rect.height)

  def DrawForeground(self, screen, window, rect, clip_rect):
    gc = window.new_gc()
    if clip_rect:
      gc.set_clip_rectangle(clip_rect.GetGdkRectangle())
    window.draw_layout(gc, self.rect.left + rect.left, self.rect.top + rect.top,
        self._layout)

  @staticmethod
  def CreateLayout(screen, caption, text_attrs):
    log_gui("CreateLayout: screen is", screen)
    gc = screen.GC(screen.window())
    context = screen.app().create_pango_context()
    if text_attrs.get("weight", None) == TextFormItem.WEIGHT_BOLD:
      b = "<b>"
      end_b = "</b>"
    else:
      b = ""
      end_b = ""
    attr_info, layout_info, accelerator = pango.parse_markup(
        '<span foreground="' + text_attrs.get('fg_color', '#000000') +
        '">' + b + EscapeHtml(caption) + end_b + '</span>', u'\x00')
    layout = screen.app().create_pango_layout(layout_info)
    layout.set_attributes(attr_info)
    desc = pango.FontDescription('%s %d' % (text_attrs.get('font', 'Helvetica'),
      text_attrs.get('size', 12)))
    layout.set_font_description(desc)
    return layout


